using System;
using System.Collections.Generic;
using System.Text;
using NBusiness.ESharp;
using NBusiness.ESharp.Compiler;
using NBusiness.CodeDom;
using NBusiness.CodeDom.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.ESharp.Testing.Properties;
using NBusiness.ESharp.Testing.Mocks;
using NBusiness.CodeDom.Services;

namespace NBusiness.ESharp.Testing
{
    [TestClass]
    public class TemplateTests
    {
        [TestMethod]
        public void TestTemplateDefineSuccess()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Template.Test1.es", Resources.Template_Test1 ),
                new EntityFile ("Template.Test2.es", Resources.Template_Test2 ));

            Assert.AreEqual(0, errorService.Errors.Count);

            Assert.IsTrue(root.Families.ContainsKey("Test"));
            Assert.AreEqual(2, root.Families["Test"].Entities.Count);

            Assert.AreEqual(1, root.Families["Test"].Entities[0].As.Count);
            Assert.AreEqual("Mock", root.Families["Test"].Entities[0].As[0].Name);
            Assert.AreEqual("NBusiness.ESharp.Testing.Mocks.MockTemplate", root.Families["Test"].Entities[0].As[0].Type.FullName);

            Assert.AreEqual(1, root.Families["Test"].Entities[1].As.Count);
            Assert.AreEqual("MockTemplate", root.Families["Test"].Entities[1].As[0].Name);
            Assert.AreEqual("NBusiness.ESharp.Testing.Mocks.MockTemplate", root.Families["Test"].Entities[1].As[0].Type.FullName);
        }

        [TestMethod]
        public void TestTemplateFail1()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Template.Test3.es", Resources.Template_Test3 ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual("ES1000", errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }
    }
}
